package utils;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

public class MapperUtil {

    // 不让创建对象
    private MapperUtil(){

    }

    private static SqlSession sqlSession; // 静态成员变量 null
    static {
        try {
            InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
            sqlSession = sqlSessionFactory.openSession(true);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 获取Mapper接口的方法
    public static <T> T  getMapper(Class<T> clazz){
        T tMapper = sqlSession.getMapper(clazz);
        return tMapper;
    }
}
